package com.zhuiyun.project.utils;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.zhuiyun.project.config.RabbitMqConfig.LIVE_EXCHANGE;

/**
 * @Description TODO
 * @Author shy
 * @Date 2024/3/9 9:29
 */
public class RabbitMqUtils {

    private static RabbitTemplate rabbitTemplate=SpringContextUtil.getBean("rabbitTemplate", RabbitTemplate.class);

    /**
     *
     * @Author shy
     * @Date 2024/3/11 11:24
      * @param message   发送消息
     * @param exchange   交换机
     * @param routingKey   路由键
     * @param TTL      过期时间
     * @return boolean
     */
    public static void sendMessage(Object message,String exchange,String routingKey,String TTL){
        CorrelationData correlationData=new CorrelationData();
        // correlationData.setId();   //全局唯一Id
        // correlationData.getFuture().addCallback   异步回调
        // rabbitTemplate.setReturnsCallback();   回调方法 返回 详细信息  失败原因  交互机  队列......
        rabbitTemplate.convertAndSend(exchange, routingKey, message, message1 -> {
            message1.getMessageProperties().setExpiration(TTL);
            return message1;
        },correlationData);
    }



}